<template>
  <div class="td-login">
    <el-row>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            placeholder="请输入手机号"
            v-model="params.Mobile"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-phone"
          ></el-input>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="params.Password"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-mima"
          ></el-input>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="17">
          <el-input
            placeholder="请输入验证码"
            v-model="params.ValidateCode"
            style="width:100%;"
            prefix-icon="icon font_family icon-td-youxiang"
          ></el-input>
        </el-col>
        <el-col :span="7">
          <img width="100%" height="40" v-bind:src="ImgByteArray" @click="loadValidateCode();">
        </el-col>
      </div>
      <div
        class="td-form-item"
        :class="{'td-form-patient-label':type===1,'td-form-doctor-label':type===0}"
      >
        <el-col :span="12">
          <router-link to="/tdRegistered">新用户注册</router-link>
        </el-col>
        <el-col :span="12" style="text-align:right;">
          <router-link to="/tdForget">忘记密码?</router-link>
        </el-col>
      </div>
      <div class="td-form-item">
        <el-col :span="24" style="text-align:center;">
          <el-button
            type="primary"
            v-if="type===1"
            class="td-btn-patient-primary"
            style="width:80%;"
            round
            @click="btnSubmit();"
          >登录</el-button>
          <el-button
            type="primary"
            v-if="type===0"
            class="td-btn-doctor-primary"
            style="width:80%;"
            round
            @click="btnSubmit();"
          >登录</el-button>
        </el-col>
      </div>
    </el-row>
  </div>
</template>

<script>
import AuthService from "../_services/auth.service";
import UserService from "../_services/user.service";
import DoctorService from "../_services/doctor.service";
import HospitalService from "../_services/hospital.service";
import wxService from "../_services/wx.service";
export default {
  name: "TianDingLoginComponent",
  data() {
    return {
      params: {
        Mobile: "",
        Password: "",
        ValidateCode: "",
        OpenID: "",
        Access_Token: ""
      },
      ImgByteArray: "",
      ValidateCode: "",
      type: 0 //0是医师 1是患者
    };
  },
  mounted() {
    this.type = parseInt(this.local$.getItem("tdType"));

    this.loadValidateCode();
  },
  methods: {
    loadValidateCode() {
      AuthService.getValidateCode().then(response => {
        if (response !== null) {
          let { Data } = response;
          this.ValidateCode = Data.ValidateCode;
          this.ImgByteArray = `data:image/jpeg;base64,${Data.ImgByteArray}`;
        }
      });
    },
    async btnSubmit() {
      if (!this.params.Mobile) {
        this.toast$("请输入手机号!");
        return;
      }

      if (!this.params.Password) {
        this.toast$("请输入密码!");
        return;
      }

      if (
        this.encry$.MD5Encryption(this.params.ValidateCode) !==
        this.ValidateCode
      ) {
        this.toast$("请输入正确的验证码!");
        return;
      }

      this.indicator$.open("请稍后...");

      let wxParams = {};

      let user = this.local$.getItem("tdUser");

      if (this.$route.query.code === undefined) {
        wxParams.code = this.local$.getItem("tdCode");
      } else {
        wxParams.code = this.$route.query.code;
      }

      let accessToken = await wxService.getOpenId(wxParams);

      this.params.OpenID = accessToken.Data.Openid;

      this.params.Access_Token = accessToken.Data.Access_Token;

      if (this.type === 0) {
        DoctorService.loginDoctor(this.params)
          .then(response => {
            let { Data } = response;
            if (!this.utils$.isEmpty(Data.IconPath)) {
              Data.IconPath = require("../assets/images/boy.png");
            }

            this.utils$.setToken(Data.Password);

            let params = {};
            params.doctorID = Data.DoctorID;
            HospitalService.getHospitalListByDoctor(params).then(
              hospitalResponse => {
                Data.hospitalList = hospitalResponse.Data;

                this.utils$.setUser(Data);
                this.utils$.setStatus(true);
                this.utils$.setCookie();

                this.toast$("登陆成功");
                this.$router.push(`/tdDoctor`);
                this.indicator$.close();
              }
            );
          })
          .catch(err => {
            wxService.getAuthorizationCode(process.env.NODE_ENV + "|/tdLogin");
          });
      } else {
        UserService.loginTD(this.params)
          .then(response => {
            let { Data } = response;

            if (this.utils$.isEmpty(Data.IconPath)) {
              Data.IconPath = require("../assets/images/gril.png");
            }

            if (this.utils$.isEmpty(Data.Birthday)) {
              Data.Age = this.utils$.getAge(Data.Birthday);
            }

            if (this.utils$.isEmpty(Data.MateBirthDay)) {
              Data.MateAge = this.utils$.getAge(Data.MateBirthDay);
            }

            this.utils$.setToken(Data.Password);
            this.utils$.setUser(Data);
            this.utils$.setStatus(true);
            this.utils$.setCookie();

            this.toast$("登陆成功");
            this.$router.push(`/tdPatient`);
            this.indicator$.close();
          })
          .catch(err => {
            wxService.getAuthorizationCode(process.env.NODE_ENV + "|/tdLogin");
          });
      }
    }
  }
};
</script>

